package br.com.informatos.sharedprefs;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	static final String USERNAME_KEY = "username";
	static final String APP_PREFS = "app_prefs";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
	
	@Override
	protected void onResume() {
		
		super.onResume();
		
		SharedPreferences prefs = getSharedPreferences(APP_PREFS, MODE_PRIVATE);
		String username = prefs.getString(USERNAME_KEY, null);
		
		TextView message = (TextView) findViewById(R.id.welcome_message);
		Button addNameButton = (Button) findViewById(R.id.add_name_Button);
		
		
		if(username != null) {
			message.setText("Bem Vindo," + username + "!");
			addNameButton.setText("Trocar de Nome");
		}else{
			message.setText("Voce Nao Caastrou Seu Nome...");
			addNameButton.setText("Adicionar Nome");
		}
		
		addNameButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, AddNameActivity.class);
				startActivity(intent);
				
			}
		});
		
	}

}
